package com.knowledgegraph.neo4j.repository;

import com.knowledgegraph.neo4j.entity.GraphRelation;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Neo4j 关系仓库
 */
@Repository
public interface GraphRelationRepository extends Neo4jRepository<GraphRelation, Long> {
    
    /**
     * 根据知识图谱ID查询关系
     */
    @Query("MATCH (s:Entity)-[r]-(t:Entity) WHERE r.graphId = $graphId RETURN r, s, t")
    List<Map<String, Object>> findByGraphId(@Param("graphId") Long graphId);
    
    /**
     * 根据关系类型查询
     */
    @Query("MATCH (s:Entity)-[r]-(t:Entity) WHERE r.type = $type RETURN r, s, t")
    List<Map<String, Object>> findByType(@Param("type") String type);
    
    /**
     * 查询两个实体之间的关系
     */
    @Query("MATCH (s:Entity)-[r]-(t:Entity) WHERE s.id = $sourceId AND t.id = $targetId RETURN r")
    List<GraphRelation> findRelationsBetween(@Param("sourceId") Long sourceId, @Param("targetId") Long targetId);
    
    /**
     * 统计各类型关系数量
     */
    @Query("MATCH ()-[r]-() WHERE r.graphId = $graphId RETURN r.type as type, count(r) as count")
    List<Map<String, Object>> countRelationsByType(@Param("graphId") Long graphId);
    
    /**
     * 查询实体的所有关系
     */
    @Query("MATCH (e:Entity)-[r]-(other:Entity) WHERE e.id = $entityId RETURN r, other")
    List<Map<String, Object>> findEntityRelations(@Param("entityId") Long entityId);
    
    /**
     * 查询强关系（强度大于阈值）
     */
    @Query("MATCH (s:Entity)-[r]-(t:Entity) WHERE r.graphId = $graphId AND r.strength > $threshold " +
           "RETURN r, s, t ORDER BY r.strength DESC")
    List<Map<String, Object>> findStrongRelations(@Param("graphId") Long graphId, @Param("threshold") Double threshold);
    
    /**
     * 创建关系
     */
    @Query("MATCH (s:Entity), (t:Entity) WHERE s.id = $sourceId AND t.id = $targetId " +
           "CREATE (s)-[r:RELATES_TO {type: $type, description: $description, strength: $strength, " +
           "confidence: $confidence, graphId: $graphId, createTime: datetime(), updateTime: datetime(), " +
           "creator: $creator, status: 'active'}]->(t) " +
           "RETURN r")
    GraphRelation createRelation(@Param("sourceId") Long sourceId, @Param("targetId") Long targetId,
                                @Param("type") String type, @Param("description") String description,
                                @Param("strength") Double strength, @Param("confidence") Double confidence,
                                @Param("graphId") Long graphId, @Param("creator") String creator);
    
    /**
     * 删除关系
     */
    @Query("MATCH ()-[r]-() WHERE r.id = $relationId DELETE r")
    void deleteRelation(@Param("relationId") Long relationId);
    
    /**
     * 更新关系属性
     */
    @Query("MATCH ()-[r]-() WHERE r.id = $relationId " +
           "SET r.type = $type, r.description = $description, r.strength = $strength, " +
           "r.confidence = $confidence, r.updateTime = datetime() " +
           "RETURN r")
    GraphRelation updateRelation(@Param("relationId") Long relationId, @Param("type") String type,
                                @Param("description") String description, @Param("strength") Double strength,
                                @Param("confidence") Double confidence);
}